<?php

namespace app\admin\controller\Order;

use app\common\controller\Backend;

class Order extends Backend
{

    protected $model = null;
    // 快速查询，搜索指定的哪些字段
    protected $searchFields = '';

    public function _initialize(){
        parent::_initialize();
        $this->model = model('Order.Order');
        $this->UavtypeModel = model('Airline.Uavtype');
        $this->MeduserModel = model('Meduser.Meduser');
    }

    // 查看
    public function index(){
        //设置过滤方法  过滤空格和标签信息
        $this->request->filter(['trim', 'strip_tags']);

        if ($this->request->isAjax()) {
            //主键
            if ($this->request->request("keyField")) {
                // 自定义搜索查询
                return $this->selectpage();
            }

            // 生成查询所需要的条件,排序方式
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with(['airline','startPoint','endPoint','type','timelist','unit','uav'])
                ->where($where)
                ->where('order.is_del',2)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['airline','startPoint','endPoint','type','timelist','unit','uav'])
                ->where($where)
                ->where('order.is_del',2)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }

    // 删除
    public function del($ids = null){
        $list = $this->model->select();
        if (!$list) {
            $this->error('订单不存在');
        }

        $data=[
            'id' => $ids,
            'is_del' => 1
        ];

        $result=$this->model->isUpdate(true)->save($data);
        if ($result === false) {
            $this->error($this->model->getError());
            exit;
        }else{
          $this->success('已放入回收站');
          exit;
        }
    }

    // 分配无人机
    public function distribute($ids=null){
        // 页面渲染
        $row=$this->model->find($ids);
        if(!$row){
            $this->error('订单不存在');
        }
        $uav_list=$this->UavtypeModel->column('id,name');


        // 提交
        if($this->request->isPost()){
            $params=$this->request->param('row/a');

            $data=[
                'id'=>$ids,
                'uav_id'=>$params['uav_id'],
            ];

            $result=$this->model->isUpdate(true)->save($data);
            if($result===false){
                $this->error($this->model->getError());
            }else{
                $this->success('分配成功');
            }
        }

        $this->assign([
            'row'=>$row,
            'uav_list' => $uav_list
        ]);
        return $this->view->fetch();
    }
    
    // 取消订单审核
    public function process($ids=null){
        // 页面渲染
        $row=$this->model->find($ids);
        if(!$row){
            $this->error('订单不存在');
        }

        // 审核提交
        if($this->request->isPost()){
            $params=$this->request->param('row/a');

            if($params['process']=='0' && $row['status']==5){
                $user_info = $this->MeduserModel->find($row['user_id']);
                if(!$user_info){
                    $this->error('用户不存在');
                }

                $data=[
                    'id'=>$ids,
                    'status'=> 4
                ];

                $result=$this->model->isUpdate(true)->save($data);
                if($result===false){
                    $this->error($this->model->getError());
                }else{
                    $this->success('审核成功');
                }
            }else{
                $data=[
                    'id'=>$ids,
                    'status'=> 6,
                    'reject_reason'=>$params['reject_reason']
                ];

                $result=$this->model->isUpdate(true)->save($data);
                if($result===false){
                    $this->error('审核失败');
                }else{
                    $this->success('审核成功');
                }
            }
        }

        $this->assign([
            'row'=>$row
        ]);
        return $this->view->fetch();
    }

}
